<!--
Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Generate Authentication</title>
</head>
<body>
<?php
include_once("../lib/component_builder.php");
$root_project_home=$_GET['project_home'];
$semf_root = "../../";

$project_home = $semf_root.$root_project_home;

$conf_dir = $project_home."/store/other/plug_ins/Auth/";
$conf_file = $conf_dir."conf.xml";

$doc = new DOMDocument();

$stored_data = array();

copy("login.xml", $project_home."/System/login.xml");

if(file_exists($conf_file))
{
	$doc->load($conf_file);
	$root = $doc->documentElement;
	
	$module_elements = $root->childNodes;
	
	foreach($module_elements as $module_element)
	{
		$module_name = $module_element->getAttribute("name");
		$stored_data[$module_name] = array();
		
		$page_elements = $module_element->childNodes;
		
		foreach($page_elements as $page_element)
		{
			$page_name = $page_element->getAttribute("name");
			$stored_data[$module_name][$page_name] = array();
			$access_elements = $page_element->childNodes;
			
			$page_xml = $project_home.$module_name."/".$page_name.".xml";
			$page_doc = new DOMDocument();
			$page_doc->load($page_xml);
			$page_doc->validate();
		
			$childs_exist = false;
			if($access_elements->length > 0)
			{
				$childs_exist = true;
			}
			$onload_code = "";
			echo "<b>Editing ${page_xml} </b>";
			if($childs_exist)
			{
				echo ": Adding ";
				$onload_code .= "\$user_type = %this_user_type;\n";
				
				$onload_code .= "if(";

			}
			$i = 0;
			foreach($access_elements as $access_element)
			{
				$access_name = $access_element->getAttribute("name");
				$stored_data[$module_name][$page_name][$access_name] = 1;
				
				echo $access_name;
				
				if($i != 0)
				{
					$onload_code .= " && ";
					echo ", ";
				}
				$onload_code .= "\$user_type != \"${access_name}\"";
				$i ++;
			}
			//closing stuff
			if($childs_exist)
			{
				$onload_code .= ")\n";
				$onload_code .= "{\n";
				
				$onload_code .= "  go2(\"System.login\");\n";
				$onload_code .= "}\n";

				//set the code..
			}
			
			$change_string = "LOGIN PLUGIN GENERATED CODE";
			$property_set = array("Action Handlers"=> 
									array("onLoad"=> "${onload_code}"));
			change_page_property($property_set, $change_string, $semf_root, $page_doc, 1);
			
			if($childs_exist)
			{
				$property_set = array("Apearance"=> 
										array("Logout"=> "1"));
				change_page_property($property_set, "", $semf_root, $page_doc, 0);
				echo ": Adding Logout";
			}
			$page_doc->save($page_xml);
			
			echo "<hr/>";
		}
	}
}
?>
</body>
</html>
